package com.platform.server.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.platform.server.service.AdvService;
import com.szrca.ima.utils.StringTools;

@Controller
public class AdvController {

	@Autowired
	private AdvService advService;
	
	/**
	 * 查看启动页
	 */
	@RequestMapping("/selectAdvList")
	public Object selectAdvList(HttpServletRequest request,String type,String id){
		SimpleDateFormat fdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> lists = advService.selectAdvList(type,id, null, null, null);
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("create_time",fdt.format(lists.get(i).get("create_time")));   
		}
		request.setAttribute("lists", lists);
		request.setAttribute("nums", lists.size());
		if(id!=null){
			return "/ui/image/startpage_info.jsp";
		}
		return "/ui/image/start_page.jsp";
	}
	
	/**
	 * 修改图片信息
	 */
	@RequestMapping("/updateImageInfo")
	public void updateImageInfo(Integer id,String image_link,String position_id, String image_url, String is_ban,HttpServletResponse response) throws Exception{
		int flag = advService.updateImageInfo(id,image_link,position_id, image_url, is_ban);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	
	/**
	 * 查看首页或发现模块轮播图
	 */
	@RequestMapping("/selectSlideshowList")
	public Object selectSlideshowList(HttpServletRequest request,String type,String id,String startTime,String endTime,String status){
		SimpleDateFormat fdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> lists = advService.selectAdvList(type, id, startTime, endTime, status);
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("create_time",fdt.format(lists.get(i).get("create_time")));   
		}
		request.setAttribute("lists", lists);
		request.setAttribute("nums", lists.size());  
		if(!StringTools.isEmpty(id)){
			if("2".equals(type)){
				return "/ui/image/index_image_info.jsp";
			}else {
				return "/ui/image/topic_image_info.jsp";
			}
		}
		if("2".equals(type)){
			return "/ui/image/index_image.jsp";
		}else {
			return "/ui/image/topic_image.jsp";
		}
	}
	
	/**
	 * 添加首页或发现轮播图
	 */
	@RequestMapping("/insertSlideshow")
	public void insertSlideshow(String image_link,String position_id,String type,String image_url,String is_ban,HttpServletResponse response) throws Exception{
		int flag = advService.insertSlideshow(image_link,position_id, type, image_url, is_ban);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	
	/**
	 * 删除首页或发现轮播图
	 */
	@RequestMapping("/deleteSlideshow")
	public Object deleteSlideshow(String idd,String type){
		@SuppressWarnings("unused")
		int flag = advService.deleteSlideshow(idd);
		return "/selectSlideshowList";
	}
	
}
